package com.aaa.controller.back;

import com.aaa.entity.Emp;
import com.aaa.service.back.DeptService;
import com.aaa.service.back.EmpService;
import com.aaa.service.back.PositionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping("/emp")
@Controller
public class EmpController {

    @Resource
    private EmpService empService;
    @Resource
    private DeptService deptService;
    @Resource
    private PositionService positionService;

    @RequestMapping("/findEmp")
    public String findEmp(Model model){
        List<Map<String, Object>> empList = empService.findEmp();
        model.addAttribute("empList",empList);
        return "back/emp-list";
    }

    @RequestMapping("/addEmpShow")
    public String addEmpShow(Model model){
        List<Map<String, Object>> deptList = deptService.findDept();
        model.addAttribute("deptList",deptList);
        List<Map<String, Object>> positionList = positionService.findPosition();
        model.addAttribute("positionList",positionList);
        return "back/emp-add";
    }

    @RequestMapping("/addEmpSubmit")
    public String addEmpSubmit(Emp emp){
        empService.addEmp(emp);
        return "redirect:/emp/findEmp";
    }

    @RequestMapping("/editEmpShow")
    public String editEmpShow(Model model,Integer empId){
        Map<String, Object> empMap = empService.findByEmpId(empId);
        model.addAttribute("empMap",empMap);
        List<Map<String, Object>> deptList = deptService.findDept();
        model.addAttribute("deptList",deptList);
        List<Map<String, Object>> positionList = positionService.findPosition();
        model.addAttribute("positionList",positionList);
        return "back/emp-edit";
    }

    @RequestMapping("/editEmpSubmit")
    public String editEmpSubmit(Emp emp){
        empService.editEmp(emp);
        return "redirect:/emp/findEmp";
    }

}
